package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.AquariumBean;
import cn.innosmart.aq.bean.SceneBean;

/**
 * Created by David on 2015/11/24.
 */
public class SceneListAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mInflater;
    private ArrayList<?> sceneList = new ArrayList<>();
    private boolean startModify;
    private View.OnClickListener onModifyClickListener;
    private AquariumBean aquariumBean;

    /**
     * 构造函数
     *
     * @param context
     * @param aquariumBean 要显示规则相应的鱼缸
     */
    public SceneListAdapter(Context context, AquariumBean aquariumBean) {
        this.context = context;
        this.aquariumBean = aquariumBean;
        mInflater = LayoutInflater.from(context);
        if (aquariumBean.getSceneList() != null)
            sceneList = aquariumBean.getSceneList();
    }

    public AquariumBean getAquariumBean() {
        return aquariumBean;
    }

    public void setAquariumBean(AquariumBean aquariumBean) {
        this.aquariumBean = aquariumBean;
    }

    @Override
    public int getCount() {
        return sceneList.size();
    }

    @Override
    public Object getItem(int position) {
        return sceneList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_scene_list, null);
            holder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.tvNoItem = (TextView) convertView.findViewById(R.id.tv_noitem);
//            holder.ivControl = (ImageView) convertView.findViewById(R.id.iv_control);
            holder.tvControl = (TextView) convertView.findViewById(R.id.tv_control);
            holder.rlChild = (RelativeLayout) convertView.findViewById(R.id.rl_child);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
//            holder.ivModify = (ImageView) convertView.findViewById(R.id.iv_modify);
//            holder.ivInfo = (ImageView) convertView.findViewById(R.id.iv_info);
            holder.tvInfo = (TextView) convertView.findViewById(R.id.tv_info);
            holder.rlBackground = (RelativeLayout) convertView.findViewById(R.id.rl_background);
            holder.tvModify = (TextView) convertView.findViewById(R.id.tv_modify);
            holder.tvDelete = (TextView) convertView.findViewById(R.id.tv_delete);
//            holder.ivDelete = (ImageView) convertView.findViewById(R.id.iv_delete);
            convertView.setTag(holder);
            AutoUtils.auto(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.rlChild.setVisibility(View.VISIBLE);
        holder.tvNoItem.setVisibility(View.INVISIBLE);
//        holder.ivControl.setImageResource(R.drawable.auto_play);
        SceneBean sceneBean = (SceneBean) sceneList.get(position);
        holder.ivIcon.setImageResource(R.drawable.rule_on);
        holder.tvTitle.setText(sceneBean.getTitle());
        holder.tvModify.setTag(sceneBean);
        holder.tvDelete.setTag(sceneBean);
//        holder.ivModify.setTag(sceneBean);
//        holder.ivDelete.setTag(sceneBean);
        holder.tvControl.setTag(sceneBean);
//        holder.ivControl.setTag(sceneBean);
        holder.tvInfo.setTag(sceneBean);
//        holder.ivInfo.setTag(sceneBean);
        if (onModifyClickListener != null) {
            holder.tvDelete.setOnClickListener(onModifyClickListener);
            holder.tvModify.setOnClickListener(onModifyClickListener);
            holder.tvControl.setOnClickListener(onModifyClickListener);
            holder.tvInfo.setOnClickListener(onModifyClickListener);
        }
        if (startModify) {
            holder.tvDelete.setVisibility(View.VISIBLE);
            holder.tvModify.setVisibility(View.VISIBLE);
            holder.tvControl.setVisibility(View.GONE);
            holder.tvInfo.setVisibility(View.GONE);
        } else {
            holder.tvDelete.setVisibility(View.GONE);
            holder.tvModify.setVisibility(View.GONE);
            holder.tvControl.setVisibility(View.VISIBLE);
            holder.tvInfo.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    /**
     * 进入修改模式
     *
     * @param startModify
     */
    public void startModify(boolean startModify) {
        this.startModify = startModify;
        notifyDataSetChanged();
    }

    /**
     * 刷新数据
     */
    public void refresh() {
        if (aquariumBean.getSceneList() != null)
            sceneList = aquariumBean.getSceneList();
        notifyDataSetChanged();
    }

    public void setOnModifyClickListener(View.OnClickListener onModifyClickListener) {
        this.onModifyClickListener = onModifyClickListener;
    }

    static class ViewHolder {
        ImageView ivIcon;
        TextView tvControl;
        TextView tvTitle;
        TextView tvInfo;
        TextView tvModify;
        TextView tvDelete;
        TextView tvNoItem;
        RelativeLayout rlChild;
        RelativeLayout rlBackground;
    }
}
